var ns, app;
app = new Vue({
    el: '#app',
    data: {
        list: [],
        form: {
            keywords: '',
            pageindex: 0,
            pagesize: 10,
            ProjectIDs: '',
            RoomIDs: '',
        },
        FullName: '',
        current_item_length: 0,
        is_searching: false,
        scroll_top: 0,
        can_scroll: false,
        searchbar_click: false,
        headerlist: []
    },
    methods: {
        get_data: function() {
            var that = this;
            that.is_searching = true;
            that.can_scroll = false;
            if (that.searchbar_click) {
                that.searchbar_click = false;
                if (that.form.keywords == '') {
                    api.toast({
                        msg: '请输入关键字',
                        duration: 2000,
                        location: 'bottom'
                    });
                    return;
                }
            }
            var options = {};
            options.pageindex = that.form.pageindex;
            options.pagesize = that.form.pagesize;
            options.ProjectIDs = that.form.ProjectIDs;
            options.RoomIDs = that.form.RoomIDs;
            options.action = "getnewroomfeelist";
            options.keywords = that.form.keywords;
            ns.post(options, function (succeed, data, err) {
                that.is_searching = false;
                if (succeed) {
                    if (data.list.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.list = data.list;
                    } else {
                        that.list = that.list.concat(data.list);
                    }
                    app.current_item_length = that.list.length;
                } else if (err) {
                    that.list = [];
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        do_select_project: function() {
            var that = this;
            ns.openWin('chooseproject_frm', '选择项目', {
                selecttype: 2
            });
        },
        do_search: function() {
            var that = this;
            that.form.pageindex = 0;
            that.searchbar_click = true;
            that.get_data();
        },
        open_wuyejf: function(item) { //账单
            var that = this;
            ns.openWin('chargeroomfee_frm', '应收账单', {
                id: item.RoomID
            });
        },
        open_resources_detail: function(item) {
            var that = this;
            ns.openWin('resources_detail_frm', '资源信息', {
                id: item.RoomID,
                edit: 1,
            });
        },
        //住户信息
        open_userinfo_detail: function (item) {
            var that = this;
            ns.openWin('userinfo_detail_frm', '住户信息详情', {
                id: item.RoomID,
            });
        },
        open_work_Details: function(item) { //工单
            var that = this;
            ns.openWin('customerservicelist_frm', '工单记录', {
                id: item.RoomID,
                servicetype: 1,
                isviewdetail: true,
                bg:"#289FFF",
                txtcolor:"#ffffff",
            });
        },
        tel_phone: function(item) {
            api.call({
                type: 'tel_prompt',
                number: item.RelatePhoneNumber
            });
        },
        clear_searchVal: function() {
            var that = this;
            document.getElementById("search-input").value = '';
        },
        get_select_project: function(data) {
            var that = this;
            that.form.ProjectIDs = data.projectids;
            that.form.RoomIDs = data.roomids;
            that.FullName = data.name;
            that.headerlist = eval('(' + data.headerlist + ')')
            setTimeout(function() {
                that.form.pageindex = 0;
                that.get_data();
                var top_h = $api.offset($api.byId('topheadercontent')).h+10
                $api.css($api.byId('topheader'), 'height:' + top_h + 'px');
                $api.css($api.byId('main_refresh'), 'margin-top:' + top_h + 'px');
            }, 500);
        },
        init_FullName: function() {
            var that = this;
            if (that.FullName == '') {
                that.FullName = '请选择资源信息';
            }
        },
        openWin: function(item, params) {
            var that = this;
            if (item.url == '') {
                return;
            }
            params = params || {
                status: -1
            };
            ns.openWin(item.url, item.name, params);
        },
        do_open_header: function(item) {
            var that = this;
            if (!item.isParent) {
                return;
            }
            ns.openWin('chooseproject_frm', '选择项目', {
                parentid: item.id,
                selecttype: 2
            });
        },
        init_search: function() {
            var that = this;
            var searchBar = document.querySelector(".aui-searchbar");
            var searchBarInput = document.querySelector(".aui-searchbar input");
            var searchBarBtn = document.querySelector(".aui-searchbar .aui-searchbar-btn");
            var searchBarClearBtn = document.querySelector(".aui-searchbar .aui-searchbar-clear-btn");
            searchBarInput.onclick = function() {
                searchBarBtn.style.display = 'block';
                searchBarBtn.style.marginRight = 0;
                searchBarBtn.textContent = "搜索";
                searchBarBtn.classList.add("aui-text-info");
                if (searchBarInput.value == '') {
                    searchBarClearBtn.style.display = 'none';
                    searchBarBtn.style.display = 'none';
                } else {
                    searchBarClearBtn.style.display = 'block';
                    searchBarBtn.style.display = 'block';
                }
            }
            searchBarInput.oninput = function() {
                if (searchBarInput.value == '') {
                    searchBarClearBtn.style.display = 'none';
                    searchBarBtn.style.display = 'none';
                } else {
                    searchBarClearBtn.style.display = 'block';
                    searchBarBtn.style.display = 'block';
                }
            }
            searchBarClearBtn.onclick = function() {
                searchBarBtn.style.display = 'none';
                this.style.display = 'none';
                that.form.keywords = '';
                searchBarBtn.classList.remove("aui-text-info");
                searchBarBtn.textContent = "";
            }
            searchBarBtn.onclick = function() {
                that.do_search();
            }
            searchBarInput.onkeypress = function(e) {
                var keycode = e.keyCode;
                if (keycode == '13') {
                    e.preventDefault();
                    searchBarInput.blur();
                    that.do_search();
                }
            }
        }
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.init_FullName();
    app.init_search();
    app.get_select_project(api.pageParam);
    api.addEventListener({
        name: 'do_choose_project_complete'
    }, function(ret) {
        app.get_select_project(ret.value);
    })
    ns.init_pull();
    api.addEventListener({
        name: 'updatewuyejf'
    }, function(ret, err) {
        app.get_data();
    });
};
